package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = menuService.findPage(queryPageBean);
            return new Result(true, "查询菜单列表成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询菜单列表失败");
        }
    }

    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
            return new Result(true, "添加菜单项成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "添加菜单项失败");
        }
    }

    @RequestMapping("/delete.do")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delete(Integer id){
        try{
            menuService.delete(id);
            return new Result(true, "删除菜单项成功, 刷新页面生效");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "删除菜单项失败");
        }
    }

    @RequestMapping("/update.do")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result update(@RequestBody Menu menu){
        try{
            menuService.update(menu);
            return new Result(true, "修改菜单项成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "修改菜单项失败");
        }
    }

    //遍历菜单信息
    @RequestMapping("/findAll.do")
    public Result findAll(){
        try{
            List<Menu> list=menuService.findAll();
            return new Result(true,"查询菜单数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询菜单数据失败");
        }
    }

    //根据id查询指定角色的菜单信息
    @RequestMapping("/findByMenuIds.do")
    public Result findByPerIds(Integer id){
        try{
            List<Menu> MenuList=menuService.findByMenuIds(id);
            List<Integer> list=new ArrayList<>();
            for (Menu  m: MenuList) {
                Integer roleId = m.getId();
                list.add(roleId);
            }
            return new Result(true,"查询菜单数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询菜单数据失败");
        }
    }
}
